<?php

namespace common\models\c2\entity;

use Yii;

/**
 * This is the model class for table "{{%lesson_daily}}".
 *
 * @property string $id
 * @property string $lesson_id
 * @property string $teacher_id
 * @property string $lesson_at
 * @property string $lesson_daytime
 * @property integer $allow_people
 * @property integer $remain_num
 * @property integer $status
 * @property string $created_at
 * @property string $updated_at
 */
class LessonDaily extends \kzo\base\models\ActiveRecord
{

    public $is_applied = 0;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%lesson_daily}}';
    }

    public function fields()
    {
        return [
            'id',
            'lesson_at',
            'lesson_daytime',
            'allow_people',
            'remain_num',
            'status',
            'is_applied',
            'teacher' => function ($model) {
                return $model->teacher->username;
            },
            'lesson' => function ($item) {
                return $item->lesson->name;
            },
            'lesson_applies' => function($item) {
            return $item->lessonApply;
            }
        ]; // TODO: Change the autogenerated stub
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['lesson_id', 'teacher_id', 'allow_people', 'remain_num'], 'integer'],
            [['lesson_at', 'lesson_daytime',], 'required'],
            [['lesson_at', 'created_at', 'updated_at'], 'safe'],
            [['lesson_at'], 'string', 'max' => 255],
            [['status'], 'integer', 'max' => 4],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'lesson_id' => Yii::t('app.c2', 'Lesson ID'),
            'teacher_id' => Yii::t('app.c2', 'Teacher ID'),
            'lesson_at' => Yii::t('app.c2', 'Lesson Date'),
            'lesson_daytime' => Yii::t('app.c2', 'Lesson Time'),
            'allow_people' => Yii::t('app.c2', 'Allow People'),
            'remain_num' => Yii::t('app.c2', 'Remain Num'),
            'status' => Yii::t('app.c2', 'Status'),
            'created_at' => Yii::t('app.c2', 'Created At'),
            'updated_at' => Yii::t('app.c2', 'Updated At'),
        ];
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\LessonDailyQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\LessonDailyQuery(get_called_class());
    }

    /**
     * setup default values
     **/
    public function loadDefaultValues($skipIfSet = true)
    {
        parent::loadDefaultValues($skipIfSet);
    }

    public function getLesson()
    {
        return $this->hasOne(Lesson::className(), ['id' => 'lesson_id']);
    }

    public function getTeacher()
    {
        return $this->hasOne(FeUser::className(), ['id' => 'teacher_id']);
    }

    public function beforeSave($insert)
    {
        if ($insert) {
            $this->remain_num = $this->allow_people;
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    public function getLessonApply()
    {
        return $this->hasMany(LessonApply::className(), ['lesson_daily_id' => 'id']);
    }

}
